﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using BOL.CentroCosto;
using System.Data.Linq;
using System.Drawing;
using Website.Logger;
using DAL;
using BOL;
using System.Web.Script.Services;
using System.Web.Services;
using UtilityExtensions;
using WebLib;


namespace UILGestion.ObraAdmin.RendicionCaja
{
    public partial class RendicionDocGrid : System.Web.UI.UserControl
    {
        public RendicionDocGrid()
        {
            lista = new ListValueBL();
        }

        private ListValueBL lista;   

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Load(string idPadre, string idColeccion)
        {
            idParent.Value = idPadre;
            this.idColeccion.Value = idColeccion;
            
            BindDataItem();
        }

       

        protected void DeleteItem(object sender, EventArgs e)
        {
            try
            {

                ImageButton lnkRemove = (ImageButton)sender;
                int idInternal = int.Parse(lnkRemove.CommandArgument.ToString());

                var list=(List<DocumentoCompraInfo>)Session[idColeccion.Value];

                var reg=list.Single(p=>p.idInternal==idInternal);

                list.Remove(reg);
                BindDataItem();
                
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
       
        protected void UpdateItem(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                

                GridView gv = ((GridView)sender);

                TextBox fechaEmisionTxt =
                   ((TextBox)gv.Rows[e.RowIndex].FindControl("fechaEmisionTxt"));
                TextBox fechaVencimientoTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("fechaVencimientoTXT"));
               

                TextBox tipoCambioTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("tipoCambioTXT"));
                
                DropDownList tipodocumentoDDL =
                    ((DropDownList)gv.Rows[e.RowIndex].FindControl("tipodocumentoDDL"));

                TextBox documentoTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("documentoTXT"));
                TextBox glosaTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("glosaTXT"));

                TextBox rucTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("rucTXT"));

                TextBox proveedorTxt =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("proveedorTxt"));

                TextBox baseImponibleTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("baseImponibleTXT"));

                TextBox baseInafectoTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("baseInafectoTXT"));


                TextBox IGVValorTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("IGVValorTXT"));

                TextBox IGVTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("IGVTXT"));

                TextBox ISCTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("ISCTXT"));

                TextBox TotalTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("TotalTXT"));
                

                TextBox honorrarioTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("honorrarioTXT"));

                TextBox ren4taTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("ren4taTXT"));

                TextBox netoHonorarioTXT =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("netoHonorarioTXT"));

               
                int idinternal = int.Parse(gv.DataKeys[e.RowIndex].Values[0].ToString());

                var lista=(List<DocumentoCompraInfo>)Session[idColeccion.Value];

                var documentoTipo = DbUtil.Db.DocumentoTipos.Single(p => p.idInternal == tipodocumentoDDL.SelectedValue.ToInt());
                var documento = lista.Single(p => p.idInternal == idinternal);
                if (documentoTipo.idInternal != 3)
                {
                    documento.setFactura(glosaTXT.Text,
                                        documentoTXT.Text,
                                        documentoTipo.pg_TIPODOC,
                                        tipodocumentoDDL.SelectedItem.Text,
                                        documentoTipo.idInternal,
                                        IGVValorTXT.Text.ToDecimal().Value,
                                        fechaEmisionTxt.Text.ToDate().Value,
                                        fechaVencimientoTXT.Text.ToDate().Value,
                                        fechaEmisionTxt.Text.ToDate().Value,
                                        tipoCambioTXT.Text.ToDecimal().Value,
                                         baseImponibleTXT.Text.ToDecimal().Value,
                                        TotalTXT.Text.ToDecimal().Value,
                                        IGVTXT.Text.ToDecimal().Value,
                                        baseInafectoTXT.Text.ToDecimal().Value,
                                        ISCTXT.Text.ToDecimal().Value);

                }
                else
                {
                    documento.SetHonorario(glosaTXT.Text,
                                        documentoTXT.Text,
                                        documentoTipo.pg_TIPODOC,
                                        tipodocumentoDDL.SelectedItem.Text,
                                        documentoTipo.idInternal,
                                        IGVValorTXT.Text.ToDecimal().Value,
                                        fechaEmisionTxt.Text.ToDate().Value,
                                        fechaVencimientoTXT.Text.ToDate().Value,
                                        fechaEmisionTxt.Text.ToDate().Value,
                                        tipoCambioTXT.Text.ToDecimal().Value,
                                        TotalTXT.Text.ToDecimal().Value,
                                        netoHonorarioTXT.Text.ToDecimal().Value,
                                        ren4taTXT.Text.ToDecimal().Value);
                }





                DbUtil.Db.SubmitChanges();
                gv.EditIndex = -1;
                itemGridView.FooterRow.Visible = true;
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void EditItem(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);
                gv.EditIndex = e.NewEditIndex;
                BindDataItem();
                setEditRow(gv.EditIndex);
                gv.FooterRow.Visible = false;
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        public void BindDataItem()
        {

            itemGridView.DataSource = (List<DocumentoCompraInfo>)Session[idColeccion.Value];
            itemGridView.DataBind();
            resetEmpty(itemGridView);
               
            
            

        }
        //private void setFooter()
        //{
        //    DropDownList tipodocumentoDDL =
        //        ((DropDownList)itemGridView.FooterRow.FindControl("tipodocumentoDDL"));

            
        //    tipodocumentoDDL.DataTextField = "descripcion";
        //    tipodocumentoDDL.DataValueField = "idInternal";
        //    tipodocumentoDDL.DataSource = new ListValueBL().GetDocumentoTipoCajaChica();
        //    tipodocumentoDDL.DataBind();
        //}
        protected void CancelItem(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);

                gv.EditIndex = -1;
                
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        private void resetEmpty(GridView gv)
        {
            if (gv.Rows.Count == 0)
            {
                List<DocumentoCompraInfo> itemList0 = new List<DocumentoCompraInfo>();
                DocumentoCompraInfo itemcero = new DocumentoCompraInfo();

                itemList0.Add(itemcero);
                gv.DataSource = itemList0;
                gv.DataBind();
                gv.Rows[0].Visible = false;
            }

        }
        private void setEditRow(int i)
        {
            


            DropDownList formaPagoDropDownList = ((DropDownList)itemGridView.Rows[i].
                                                FindControl("tipodocumentoDDL"));

            TextBox idTipodocumento = ((TextBox)itemGridView.Rows[i].
                                                FindControl("idTipoDocumentoTXT"));

            //Input glosa = ((Input)itemGridView.Rows[i].
            //                                    FindControl("descripcionTextBox"));
         
             

            formaPagoDropDownList.DataValueField = "idInternal";
            formaPagoDropDownList.DataTextField = "descripcion";
            formaPagoDropDownList.DataSource = lista.GetDocumentoTipoRecepcionDoc();
            formaPagoDropDownList.DataBind();


            formaPagoDropDownList.SelectedValue = idTipodocumento.Text;
                 

        }
    }
}